package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;

    @RequestMapping("/list")
    @RequiredPermission(name = "员工页面",expremission = "employee:list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo){//因为要看到他的数据,所以要放在作用域里面,所以得用到它的addattribute
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        List<Department> departments = departmentService.listall();
        model.addAttribute("departments",departments);

        return "employee/list";//因为已经配置了视图解析器,所以省略了前面的WEB-INF/views/ 和后面的.jsp
    }

    @RequiredPermission(name = "员工删除",expremission = "employee:delete")
    @RequestMapping("/delete")
    public String delete(Long id){
        if(id !=null){
            employeeService.delete(id);
        }
        return "redirect:/employee/list";//就像以前一样,删除后要除定向会list查找全部的页面
    }

    @RequiredPermission(name = "员工新增/编辑",expremission = "employee:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id,Model model){
        List<Department> departments = departmentService.listall();
        model.addAttribute("departments",departments);

        model.addAttribute("roles",roleService.listall());

        if (id !=null){
            model.addAttribute("employee",employeeService.get(id));
        }
        return "employee/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name = "员工新增/编辑",expremission = "employee:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee, Long[]ids){
            if(employee.getId()!=null){//判断你进来的是否带有id,如果带有id的就肯定是修改的了
                employeeService.update(employee,ids);
            }
            else {
                employeeService.save(employee,ids);
            }
        return new JsonResult();
    }




  @RequestMapping("/checkName")
  @ResponseBody
  public Map checkName(String username){
      HashMap<String, Boolean> map = new HashMap<>();
      Employee employee = employeeService.selectByUserName(username);
      map.put("valid",employee==null);
      return map;
  }

    @RequiredPermission(name = "员工导出",expremission = "employee:exportXls")
    @RequestMapping("/exportXls")
  public void exportXls(HttpServletResponse response) throws IOException {
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");
        Workbook workbook = employeeService.exprtXls();

        workbook.write(response.getOutputStream());
    }

    @RequiredPermission(name = "员工导入",expremission = "employee:importXls")
    @RequestMapping("/importXls")
    public String importXls(MultipartFile file) throws IOException {
      employeeService.importExxls(file);
      return "redirect:/employee/list";//就像以前一样,删除后要除定向会list查找全部的页面

    }

    @RequestMapping("/updatePassword")
    public String updatePassword(){
        return "/common/updatePassword";
    }

     @RequestMapping("/checkPassword")
    //@RequiredPermission(name = "员工新增/编辑",expremission = "employee:saveOrUpdate")
     @ResponseBody
    public JsonResult checkPassword(Long id,String oldPassword) {
         try {
             employeeService.checkPassword(id, oldPassword);
             return new JsonResult();
         } catch (Exception e) {
             e.printStackTrace();
             return new JsonResult(false, e.getMessage());
         }
     }

     @RequestMapping("/updatePwd")
     @ResponseBody
     public JsonResult updatepwd(Long id,String newPassword){
            employeeService.updatepwd(id,newPassword);
            return new JsonResult();
         }


    @RequestMapping("/solfdelete")
    @RequiredPermission(name = "员工禁用",expremission = "employee:solfdelete")
    @ResponseBody
    public JsonResult solfdelete(Long id){
        boolean b = employeeService.selectBystatusId(id);
        employeeService.updatestatus(id,!b);
        return new JsonResult();
    }

    @RequestMapping("/resetpassword")
    @ResponseBody
    public JsonResult resetpassword(Long id){
        employeeService.resetpassword(id);
        return new JsonResult();
    }

    @RequestMapping("/deleteAll")
    @ResponseBody
    public JsonResult deleteAll(@RequestParam("ids[]") Long[] ids){
        employeeService.deleteAll(ids);
        return new JsonResult();
    }
}